﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace EasyCommServer.Core
{
    public partial class Client
    {
        private int login(string username, string password)
        {
            int nRet = -1;
            bool bLoginSuccess = false;
            SqlConnection sqlconn = new SqlConnection();
            sqlconn.ConnectionString = SharedDefines.ConnectionString;
            try
            {
                string szQuery = "select * from login where username = '" + username + "'";
                SqlCommand cmd = new SqlCommand(szQuery, sqlconn);
                //MessageBox.Show(szQuery);
                cmd.CommandType = CommandType.Text;
                sqlconn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    int pwdOrdinal = dr.GetOrdinal("password");
                    string pwd = dr.GetString(pwdOrdinal);
                    if (pwd == password)
                    {
                        bLoginSuccess = true;
                        break;
                    }
                }
                if (bLoginSuccess)
                {
                    nRet =  IM_OK;
                }
                else
                {
                    nRet = IM_WrongPass;
                }
                dr.Close();

            }
            catch (System.Exception ex)
            {
                //MessageBox.Show(ex.Message);
                Console.WriteLine(ex.Message);

            }
            finally
            {

                if (sqlconn.State == ConnectionState.Open)
                {
                    sqlconn.Close();
                }
            }
            return nRet;
        }
    }
}
